﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCTLib.Common;
using DCTAccess.Business.Needs;
using DCTAccess.Business.Member;
using BDS;

public partial class Members_mua_banner : System.Web.UI.Page
{
    BannerUI objNeeds = new BannerUI();
    protected void Page_Load(object sender, EventArgs e)
    {
        Upload2.UploadFolderPath = "~/Upload/Banner";
        Upload2.OnUploadCompleted += Upload2_OnUploadCompleted;
        if (!IsPostBack)
        {
            LoadServices();
        }
    }

    private void Upload2_OnUploadCompleted(string selectedFile)
    {
        txtProductImage.Text = selectedFile.Substring(2).ToString();
        if (txtProductImage.Text != "")
        { }
    }

    protected void btnAddNewBanner_Click(object sender, EventArgs e)
    {
        if (CheckValid())
        {
            //lblMessage.Text += txtTitle.Text +"</br>";
            //lblMessage.Text += txtLink.Text + "</br>";
            //lblMessage.Text += txtProductImage.Text + "</br>";
            //lblMessage.Text += ddlServices.SelectedValue.ToString() + "</br>";            
            try
            {
                Member memInfo = MemberController.GetMemberByUsername(Session["User"].ToString());
                int userID = memInfo.ID; 

                int result = objNeeds.Add(txtTitle.Text.Trim(), txtProductImage.Text.Trim(), txtLink.Text.Trim(),
                        userID, Convert.ToInt32(ddlServices.SelectedValue.ToString()));

                if (result > 0)
                {
                    new BDS.ProductUI().ExecCalculatePoint(memInfo.UserName.ToString(), 2600, result);                    
                    lblMessage.Text = "File đã gửi thành công.";
                    ReInsertData();
                }
                else
                {
                    lblMessage.Text = "Upload lỗi";
                }
            }
            catch (System.Exception ex)
            {
                lblMessage.Text = "Server quá tải,xin vui lòng thử lại";
            }
        }
    }

    private void ReInsertData()
    {
        txtTitle.Text = "";
        txtLink.Text = "";
        txtProductImage.Text = "";
    }

    private bool CheckValid()
    {
        bool validated = true;
        if (ValidationHelper.IsEmpty(txtTitle.Text))
        {
            lblMessage.Text = "Tiêu đề banner không được bỏ trống";
            return false;
        }
        return validated;
    }

    private void LoadServices()
    {
        ControlLoader.LoadDropDownList(this.ddlServices, NeedController.GetServices(), "ServiceID", "ServiceName");
    }

    private Need GetNeed()
    {
        Need need = new Need();
        //need.NeedName = txtNeed.Text;
        //need.CategoryPageId = int.Parse(ddlServices.SelectedValue);
        //need.IsEnable = cbxAction.Checked;

        return need;
    }
}
